home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SAMPLES / OGLINFO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-26  |  5.4 KB  |  216 lines

  1. /* oglinfo.c */
  2.  
  3. /* This demo modified by BrianP to accomodate Mesa and test
  4.  * the GLX 1.1 functions.
  5.  */
  6.  
  7.  
  8.  
  9. #include <GL/gl.h>
  10. #include <GL/glx.h>
  11. #include <GL/glu.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. int visual_request0[] = { None }; /* don't need much of a visual */
  16. int visual_request1[] = { GLX_RGBA, None }; /* in case CI failed */
  17.  
  18. void main(int argc, char **argv)
  19. {
  20.   char *display_name = NULL;
  21.   char *string;
  22.   Display       *dpy;
  23.   int           screen_num;
  24.   int           major, minor;
  25.   XVisualInfo   *vis;
  26.   GLXContext    ctx;
  27.   Window        root,  win;
  28.   Colormap      cmap;
  29.   XSetWindowAttributes swa;
  30.   int dontcare;
  31.  
  32.   /* parse arguments */
  33.   if(argc > 1)
  34.     if(!strcmp(argv[1],"-display"))
  35.       display_name = argv[2];
  36.     else {
  37.       fprintf(stderr, "Usage: %s [-display <display>]\n",argv[0]);
  38.       return;
  39.     }
  40.  
  41.   /* get display */
  42.   if (!(dpy = XOpenDisplay(display_name))) {
  43.     fprintf(stderr,"Error: XOpenDisplay() failed.\n");
  44.     return;
  45.   }
  46.  
  47.   /* does the server know about OpenGL & GLX? */
  48. #ifndef MESA
  49.   if(!XQueryExtension(dpy, "GLX", &dontcare, &dontcare, &dontcare)) {
  50.     fprintf(stderr,"This system doesn't appear to support OpenGL\n");
  51.     return;
  52.   }
  53. #endif
  54.  
  55.   /* find the glx version */
  56.   if(glXQueryVersion(dpy, &major, &minor))
  57.     printf("GLX Version: %d.%d\n", major, minor);
  58.   else {
  59.     fprintf(stderr, "Error: glXQueryVersion() failed.\n");
  60.     return;
  61.   }
  62.  
  63.   /* get screen number */
  64.   screen_num = DefaultScreen(dpy);
  65.  
  66. /* This #ifdef isn't redundant. It keeps the build from breaking
  67. ** if you are building on a machine that has an old (1.0) version
  68. ** of glx.
  69. **
  70. ** This program could still be *run* on a machine that has an old 
  71. ** version of glx, even if it was *compiled* on a version that has
  72. ** a new version.
  73. **
  74. ** If compiled on a system with an old version of glx, then it will 
  75. ** never recognize glx extensions, since that code would have been
  76. ** #ifdef'ed out.
  77. */
  78. #ifdef GLX_VERSION_1_1
  79.  
  80.   /*
  81.   ** This test guarantees that glx, on the display you are inquiring,
  82.   ** suppports glXQueryExtensionsString().
  83.   */
  84.   if(minor > 0 || major > 1)
  85.     string = (char *) glXQueryExtensionsString(dpy, screen_num);
  86.   else
  87.     string = "";
  88.  
  89.   if(string)
  90.     printf("GLX Extensions (client & server): %s\n",
  91.        string);
  92.   else {
  93.     fprintf(stderr, "Error: glXQueryExtensionsString() failed.\n");
  94.     return;
  95.   }
  96.  
  97.   if (minor>0 || major>1) {
  98.      printf("glXGetClientString(GLX_VENDOR): %s\n", glXGetClientString(dpy,GLX_VENDOR));
  99.      printf("glXGetClientString(GLX_VERSION): %s\n", glXGetClientString(dpy,GLX_VERSION));
  100.      printf("glXGetClientString(GLX_EXTENSIONS): %s\n", glXGetClientString(dpy,GLX_EXTENSIONS));
  101.      printf("glXQueryServerString(GLX_VENDOR): %s\n", glXQueryServerString(dpy,screen_num,GLX_VENDOR));
  102.      printf("glXQueryServerString(GLX_VERSION): %s\n", glXQueryServerString(dpy,screen_num,GLX_VERSION));
  103.      printf("glXQueryServerString(GLX_EXTENSIONS): %s\n", glXQueryServerString(dpy,screen_num,GLX_EXTENSIONS));
  104.   }
  105.  
  106.  
  107. #endif
  108.  
  109.    /* get any valid OpenGL visual */
  110.    if (!(vis = glXChooseVisual(dpy, screen_num, visual_request0)))  {
  111.       if (!(vis = glXChooseVisual(dpy, screen_num, visual_request1)))  {
  112.          fprintf(stderr,"Error: glXChooseVisual() failed.\n");
  113.          return;
  114.       }
  115.    }
  116.  
  117.    /* get context */
  118.    ctx = glXCreateContext(dpy,vis,0,GL_TRUE);
  119.  
  120.    /* root window */
  121.    root = RootWindow(dpy,vis->screen);
  122.  
  123.    /* get RGBA colormap */
  124.    cmap = XCreateColormap(dpy, root, vis->visual, AllocNone);
  125.  
  126.    /* get window */
  127.    swa.colormap = cmap;
  128.    swa.border_pixel = 0;
  129.    swa.event_mask = StructureNotifyMask;
  130.    win = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, vis->depth,
  131.                InputOutput,vis->visual,
  132.                CWBorderPixel|CWColormap|CWEventMask,
  133.                &swa);
  134.  
  135.    glXMakeCurrent(dpy,win,ctx);
  136.  
  137.   string = (char *) glGetString(GL_VERSION);
  138.   if(string)
  139. #ifdef MESA
  140.     printf("Mesa Version: %s\n", string);
  141. #else
  142.     printf("OpenGL Version: %s\n", string);
  143. #endif
  144.   else {
  145.     fprintf(stderr, "Error: glGetString(GL_VERSION) failed.\n");
  146.     return;
  147.   }
  148.  
  149.   string = (char *) glGetString(GL_EXTENSIONS);
  150.  
  151.   if(string)
  152. #ifdef MESA
  153.     printf("Mesa Extensions: %s\n", string);
  154. #else
  155.     printf("OpenGL Extensions: %s\n", string);
  156. #endif
  157.   else {
  158.     fprintf(stderr, "Error: glGetString(GL_EXTENSIONS) failed.\n");
  159.     return;
  160.   }
  161.  
  162.   string = (char *) glGetString(GL_RENDERER);
  163.  
  164.   if(string)
  165. #ifdef MESA
  166.     printf("Mesa Renderer: %s\n", string);
  167. #else
  168.     printf("OpenGL renderer: %s\n", string);
  169. #endif
  170.   else {
  171.     fprintf(stderr, "Error: glGetString(GL_RENDERER) failed.\n");
  172.     return;
  173.   }
  174.  
  175. /*
  176. ** This #ifdef prevents a build failure if you compile on an a
  177. ** machine with an old GLU library. 
  178. **
  179. ** If you build on a pre GLU 1.1 machine, you will never be able
  180. ** to get glu info, even if you run on a GLU 1.1 or latter machine,
  181. ** since the code has been #ifdef'ed out.
  182. */
  183. #ifdef GLU_VERSION_1_1
  184.  
  185.   /*
  186.   ** If the glx version is 1.1 or latter, gluGetString() is guaranteed
  187.   ** to exist.
  188.   */
  189.   if(minor > 0 || major > 1)
  190.     string = (char *) gluGetString(GLU_VERSION);
  191.   else
  192.     string = "1.0";
  193.  
  194.   if(string)
  195.     printf("GLU Version: %s\n", string);
  196.   else {
  197.     fprintf(stderr, "Error: gluGetString(GLU_VERSION) failed.\n");
  198.     return;
  199.   }
  200.   
  201.   if(minor > 0 || major > 1)
  202.     string = (char *) gluGetString(GLU_EXTENSIONS);
  203.   else
  204.     string = "";
  205.  
  206.   if(string)
  207.     printf("GLU Extensions: %s\n", string);
  208.   else {
  209.     fprintf(stderr, "Error: gluGetString(GLU_EXTENSIONS) failed.\n");
  210.     return;
  211.   }
  212.  
  213.  
  214. #endif
  215. }
  216.